package com.zqcy.workbench;

import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.bambooclod.epassbase.api.EpassBaseSDK;
import com.onemdos.banner.OneMDOSBanner;
import com.onemdos.base.OneMDOS;
import com.onemdos.base.OneMDOSCallback;
import com.onemdos.base.OneMDOSContext;
import com.onemdos.base.OneMDOSMessageCallback;
import com.onemdos.base.message.model.OneMDOSMessage;
import com.onemdos.configuration.MDOSConfiguration;
import com.onemdos.crashreport.MDOSCrashReport;
import com.onemdos.performance.MDOSPerformance;
import com.onemdos.portal.config.PortalConfig;
import com.onemdos.push.OneMDOSAgent;
import com.onemdos.push.OneMDOSPush;
import com.onemdos.push.callback.OneMDOSRegisterListener;
import com.onemdos.share.OneMDOSShareConfig;
import com.onemdos.share.OneMDOSSharePlatformConfig;
import com.onemdos.upgrade.MDOSUpgrade;
import com.onemods.logger.MDOSLogger;
import com.shinemo.base.core.utils.LogUtil;
import com.shinemo.minisdk.config.MiniSdk;
import com.shinemo.minisdk.util.HwCloudUtils;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;

import java.util.HashMap;
import java.util.UUID;

public class MyApplication extends Application {

    private static Application mInstance;
    //小程序-华为测试环境
//    public static final String HOST = "https://hw-mdos.uban360.com:3301/";
    //小程序-本地测试环境
    public static final String HOST = "http://mdos.uban360.net/";

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        EpassBaseSDK.Builder()
                .init(this)
                .setAppID("OneMdos")
                .setOperator("appUsers:sdk")
                .setAuthKey("1234567890123456")
//                .setBaseUrl("https://59.36.10.37:10046/epass-api/")
                .setBaseUrl("https://123.60.230.30:10046/epass-api/")
                .setHttpRequestTimeOut(5);

        //华为演示环境
//        OneMDOS.init(this, "897435556827365376", "Project897435389944397824");
//        OneMDOS.setServerAddress("114.116.228.37", 3302);

        //基础组件-华为测试环境
        //3305环境
//        OneMDOS.init(this, "919260114232279040", "Project919162311766179840");
//        OneMDOS.setServerAddress("114.116.228.37", 3309);
        //3301环境
//        OneMDOS.init(this, "886273163913068544", "project886272961143635968");
//        OneMDOS.setServerAddress("114.116.228.37", 3302);

        //基础组件-本地测试环境
        OneMDOS.init(this, "87781003", "1");
        OneMDOS.setServerAddress("10.0.10.195", 13000);
        /**
         * 设置业务id
         */
        OneMDOS.setUUid("10001");

        //版本更新初始化
        MDOSUpgrade.init(this);
        //广告初始化
        OneMDOSBanner.init(this);
        //门户初始化 传入页面id
        PortalConfig.getInstance().init(this);

        //分享初始化
        OneMDOSShareConfig.init(this);
        //微信分享key
        OneMDOSSharePlatformConfig.setWeiXIn("wxbdb0cec71f5d97ad");
        //qq分享key
        OneMDOSSharePlatformConfig.setQQ("1105583692");
        //微博分享key
        OneMDOSSharePlatformConfig.setWB("123456");

        //小程序初始化
        MiniSdk.init(this, HOST);
        MiniSdk.setAppStackCacheSize(5);
        //小程序华为云环境配置
        HwCloudUtils.getInstance().initHwCloud("M4CQO1KLRJOZRMHWIQCU", "UZdlHQMYS8VCMD7CuvtX0MNhp1uVpXB6vauME16B", "0657e5ca6e000fb82f33c01ee89bd2f1", "sis-ext.cn-north-4.myhuaweicloud.com");
        //事件上报初始化
        MDOSLogger.initRegister(this);
        //性能分析库初始化(目前为止管理后台只能显示统计数量，显示堆栈还有问题，客户端想看到效果的话可以先在logcat里面查看，
        // 用MDOSPerformance这个关键词来做过滤)
        MDOSPerformance.init();
        //崩溃日志收集，目前只支持收集jvm层的崩溃，native层(c或c++)的不支持(目前为止管理后台只能显示统计数量，显示堆栈还有问题，
        // 客户端想看到效果的话可以先在logcat里面查看，用MDOSCrashReport这个关键词来做过滤,这个过滤的是上报接口)
        MDOSCrashReport.getInstance().init(this);
        //远程配置初始化
        MDOSConfiguration.init(this);
        //推送初始化
        OneMDOSContext.getInstance().setContext(this);
//        OneMDOSPush.setXiaomiParam(PushConstans.XM_APP_ID, PushConstans.XM_APP_KEY);
//        OneMDOSPush.setOppoParam(PushConstans.OPPO_APP_KEY, PushConstans.OPPO_APP_Secret);
//        OneMDOSPush.setMeiZuParam(PushConstans.MZ_APP_ID, PushConstans.MZ_APP_KEY);
        OneMDOSAgent oneMDOSAgent = OneMDOSAgent.getInstance(this);
        oneMDOSAgent.register(new OneMDOSRegisterListener() {
            @Override
            public void onSuccess(String token) {
                Log.i("Push", "@@@@ success token: " + token);
            }

            @Override
            public void onFailed(String errCode, String errDesc) {
                Log.i("Push", "@@@@ fail: " + errCode + errDesc);
            }
        });
        OneMDOS.setOnMessageCallback(new OneMDOSMessageCallback() {
            @Override
            public void onGetPushId(String pushId) {
                LogUtil.e("ONEMDOS", "pushId=" + pushId);
            }

            @Override
            public void onGetMessage(OneMDOSMessage message) {
                LogUtil.e("ONEMDOS", "online onGetMessage ===>  " + message.getContent());
            }
        });

        try {
            // 在调用TBS初始化、创建WebView之前进行如下配置
            initX5();
            QbSdk.setDownloadWithoutWifi(true);
            QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
                @Override
                public void onCoreInitFinished() {

                }

                @Override
                public void onViewInitFinished(boolean b) {

                }
            });


        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private void initX5() {
        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);
    }

    public static Application getInstance() {
        return mInstance;
    }
}
